package com.daichi.yygh.order.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daichi.yygh.common.result.Result;
import com.daichi.yygh.common.utils.AuthContextHolder;
import com.daichi.yygh.enums.OrderStatusEnum;
import com.daichi.yygh.model.order.OrderInfo;
import com.daichi.yygh.order.service.OrderService;
import com.daichi.yygh.vo.order.OrderCountQueryVo;
import com.daichi.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "用户订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    private final OrderService orderService;

    @Autowired
    public OrderApiController(OrderService orderService) {
        this.orderService = orderService;
    }

    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(orderId);
    }

    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrder(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation(value = "分页查询订单列表")
    @GetMapping("auth/{current}/{limit}")
    public Result getOrderPage(@PathVariable Integer current,
                               @PathVariable Integer limit,
                               OrderQueryVo orderQueryVo,
                               HttpServletRequest request) {
        Page<OrderInfo> page = new Page<>(current, limit);
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> result = orderService.selectPage(page, orderQueryVo);
        return Result.ok(result);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId) {
        boolean success = orderService.cancelOrder(orderId);
        return Result.ok(success);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }


}
